package com.by.filter;

import org.slf4j.MDC;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Order(1)
@WebFilter
public class TraceIdFliter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest =(HttpServletRequest) request;
        String traceId = httpServletRequest.getHeader("traceId");
        //线程脑门上帖标签
        MDC.put("traceId", traceId);
        // 放行
        try {
            chain.doFilter(request,response);
        } finally {
            MDC.clear();
        }

    }
}
